﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Hosting;
using System.IO;
using System.ServiceModel.Activation;
using CommunitySystem.BO;

namespace CommunitySystem.Web.Service
{
    
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class UploadService : IUploadService
    {
        private string _tempExtension = "_temp";//"_temp";

        #region IUploadService Members

        /// <summary>
        /// 得到上传文件路径
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="firstChunk"></param>
        /// <param name="lastChunk"></param>
        /// <returns></returns>
        public string GetServerFilePath(string fileName, bool firstChunk, bool lastChunk)
        {
            string uploadFolder = GetUploadFolder();
            //string tempFileName = fileName + _tempExtension;
            string serverFileName = uploadFolder + @"\" + fileName;
            return serverFileName;
        }

        /// <summary>
        /// 暂停上传
        /// </summary>
        /// <param name="filename"></param>
        public void AbortUpload(string filename)
        {

        }

        /// <summary>
        /// 取消上传
        /// </summary>
        /// <param name="fileName"></param>
        public void CancelUpload(string fileName)
        {
            string uploadFolder = GetUploadFolder();
            string tempFileName = fileName + _tempExtension;

            if (File.Exists(uploadFolder + @"\" + tempFileName))
                File.Delete(uploadFolder + @"\" + tempFileName);
        }

        public void StoreFileAdvanced(string fileName, byte[] data, int dataLength, string parameters, bool firstChunk, bool lastChunk)
        {
            
            try
            {
                string uploadFolder = GetUploadFolder();
                string tempFileName = fileName + _tempExtension;

                //当上传文件的第一批数据时，先清空以往的相同文件名的文件（同名文件可能为上传失败造成）
                if (firstChunk)
                {
                    ////删除临时文件
                    //if (File.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName))
                    //    File.Delete(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName);

                    //删除目标文件
                    if (File.Exists(uploadFolder + @"\" + fileName))
                        File.Delete(uploadFolder + @"\" + fileName);


                }


                FileStream fs = File.Open(uploadFolder + @"\" + tempFileName, FileMode.Append);
                fs.Write(data, 0, dataLength);
                fs.Close();

                if (lastChunk)
                {
                    //将临时文件重命名为原来的文件名称
                    File.Move(uploadFolder + @"\" + tempFileName, uploadFolder + @"\" + fileName);

                    //Finish stuff....
                   // FinishedFileUpload(fileName, parameters);
                }
            }
            catch (Exception ex)
            {
                Log4JClass.SetErro("上传附件",ex);
               
            }
        }

        /// <summary>
        /// 删除上传文件
        /// </summary>
        /// <param name="fileName"></param>
        protected void DeleteUploadedFile(string fileName)
        {
            string uploadFolder = GetUploadFolder();

            if (File.Exists(uploadFolder + @"\" + fileName))
                File.Delete(uploadFolder + @"\" + fileName);
        }

        protected virtual void FinishedFileUpload(string fileName, string parameters)
        {
        }

        /// <summary>
        /// 获取上传路径
        /// </summary>
        /// <returns></returns>
        protected virtual string GetUploadFolder()
        {
           // return @HostingEnvironment.ApplicationPhysicalPath + "/" + "Upload";
            //return @"D:\成功的例子\Upload";

            string uploadFolder=System.Web.Configuration.WebConfigurationManager.AppSettings["file"].ToString();
            //目录不存在则新建
            if (!System.IO.Directory.Exists(uploadFolder))
            {
                System.IO.Directory.CreateDirectory(uploadFolder);
            }
            return uploadFolder;
        }


        #endregion
    }
}
